﻿// // ******************************************************************
// //       /\ /|       @file       CacheService.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2025-03-02 12:03
// //    *(__\_\        @Copyright  Copyright (c) , 2025-02-23 10:02 Await
// // ******************************************************************

using AutomaticSubtitles.Contracts.Dto;
using AutomaticSubtitles.Contracts.Vo;
using AutomaticSubtitles.Service.Application.Cahce.Commands;
using AutomaticSubtitles.Service.Application.Cahce.Queries;

namespace AutomaticSubtitles.Service.Services;

[Tags("通用配置")]
public class CacheService : BaseService<CacheService>
{
    [Tags("任务")]
    [EndpointDescription("执行任务")]
    [EndpointName("执行任务")]
    [EndpointSummary("执行任务")]
    [Authorize]
    public async Task<ResponseModel<string>> UpdateCachedData(string name)
    {
        var updateCacheDataCommand = new UpdateCacheDataCommand(name);
        await PublishAsync(updateCacheDataCommand);
        return updateCacheDataCommand.Result.CreateResult();
    }

    [Tags("任务")]
    [EndpointDescription("获取任务")]
    [EndpointName("获取任务")]
    [EndpointSummary("获取任务")]
    [Authorize]
    public async Task<ResponseModel<List<TaskDto>>> GetTaskList()
    {
        var updateCacheDataCommand = new GetCacheTaskListQuery();
        await PublishAsync(updateCacheDataCommand);
        return updateCacheDataCommand.Result.CreateResult()!;
    }

    [EndpointDescription("添加自定义配置")]
    [EndpointName("添加自定义配置")]
    [EndpointSummary("添加自定义配置")]
    [Authorize]
    public async Task<ResponseModel<int>> AddCacheKey(CacheKeyVo data)
    {
        var updateCacheDataCommand = new AddCacheKeyCommand(data);
        await PublishAsync(updateCacheDataCommand);
        return updateCacheDataCommand.Result.CreateResultAdd();
    }

    [EndpointDescription("修改自定义配置")]
    [EndpointName("修改自定义配置")]
    [EndpointSummary("修改自定义配置")]
    [Authorize]
    public async Task<ResponseModel<int>> UpdateCacheKey(CacheKeyVo data)
    {
        var updateCacheDataCommand = new UpdateCacheKeyCommands(data);
        await PublishAsync(updateCacheDataCommand);
        return updateCacheDataCommand.Result.CreateResultUpdate();
    }

    [EndpointDescription("删除自定义配置")]
    [EndpointName("删除自定义配置")]
    [EndpointSummary("删除自定义配置")]
    [Authorize]
    public async Task<ResponseModel<int>> DeleteCacheKey(string id)
    {
        var updateCacheDataCommand = new DeleteCacheKeyCommands(id);
        await PublishAsync(updateCacheDataCommand);
        return updateCacheDataCommand.Result.CreateResultDelete();
    }

    [EndpointDescription("获取所有配置")]
    [EndpointName("获取所有配置")]
    [EndpointSummary("获取所有配置")]
    [Authorize]
    public async Task<ResponseModel<IReadOnlyList<CacheKeyDto>?>> GetCacheKeyList()
    {
        var updateCacheDataCommand = new GetCacheKeyListQuery();
        await PublishAsync(updateCacheDataCommand);
        return updateCacheDataCommand.Result.CreateResult();
    }

    [Tags("媒体配置")]
    [EndpointDescription("获取媒体目录配置")]
    [EndpointName("获取媒体目录配置")]
    [EndpointSummary("获取媒体目录配置")]
    [Authorize]
    public async Task<ResponseModel<MediaConfigurationDto>> GetMediaDirectoryConfiguration()
    {
        var updateCacheDataCommand = new GetCacheKeyGetMediaDirectoryConfigurationListQuery();
        await PublishAsync(updateCacheDataCommand);
        return updateCacheDataCommand.Result.CreateResult();
    }

    [Tags("媒体配置")]
    [EndpointDescription("修改媒体目录配置")]
    [EndpointName("修改媒体目录配置")]
    [EndpointSummary("修改媒体目录配置")]
    [Authorize]
    public async Task<ResponseModel<int>> UpdateMediaDirectoryConfiguration(MediaConfigurationVo data)
    {
        var updateCacheDataCommand = new UpdateCacheKeyGetMediaDirectoryConfigurationListQuery(data);
        await PublishAsync(updateCacheDataCommand);
        return updateCacheDataCommand.Result.CreateResult();
    }

    [Tags("百度配置")]
    [EndpointSummary("获取百度翻译API配置")]
    [EndpointName("获取百度翻译API配置")]
    [EndpointDescription("获取百度翻译API基本配置")]
    [Authorize]
    public async Task<ResponseModel<BaiduTranslationAPIDto>> GetBaiduTranslationAPIConfiguration()
    {
        var updateCacheDataCommand = new GetCacheKeyBaiduTranslationAPIConfigurationQuery();
        await PublishAsync(updateCacheDataCommand);
        return updateCacheDataCommand.Result.CreateResult()!;
    }

    [Tags("AI配置")]
    [EndpointDescription("获取ai配置")]
    [EndpointName("获取ai配置")]
    [EndpointSummary("获取ai配置")]
    [Authorize]
    public async Task<ResponseModel<AIConfigModelDto?>> GetAIConfiguration()
    {
        var command = new GetAiConfigQuery();
        await PublishAsync(command);
        var aiConfigModelDto = new AIConfigModelDto()
        {
            Enabled = command.Enabled,
            ApiKey = command.Result,
            BaseUrl = command.Path,
            Model = command.Model
        };
        return aiConfigModelDto.CreateResult();
    }

    [Tags("AI配置")]
    [EndpointDescription("更新AI配置")]
    [EndpointName("更新AI配置")]
    [EndpointSummary("更新AI配置")]
    [Authorize]
    public async Task<ResponseModel<int>> UpdateAIConfiguration(AIConfigModelVo data)
    {
        var command = new UpdateAiDataCommand(data.enabled, data.baseUrl, data.apiKey, data.model);
        await PublishAsync(command);
        return command.Result.CreateResultAdd_Msg();
    }

    [Tags("AI配置")]
    [EndpointDescription("获取提示词")]
    [EndpointName("获取提示词")]
    [EndpointSummary("获取提示词")]
    [Authorize]
    public async Task<ResponseModel<string?>> GetModifyPromptWordQuery()
    {
        var command = new GetModifyPromptWordQuery();
        await PublishAsync(command);
        return command.Result.CreateResult();
    }

    [Tags("AI配置")]
    [EndpointDescription("修改提示词")]
    [EndpointName("修改提示词")]
    [EndpointSummary("修改提示词")]
    [Authorize]
    public async Task<ResponseModel<int>> UpdateModifyPromptWordQuery(string promptWord)
    {
        var command = new UpdateModifyPromptWordCommand(promptWord);
        await PublishAsync(command);
        return command.Result.CreateResultUpdate_Msg();
    }

    [Tags("百度配置")]
    // [EndpointName("修改百度翻译API配置1")]
    [EndpointSummary("修改百度翻译API配置")]
    [EndpointDescription("修改百度翻译API配置")]
    [Authorize]
    public async Task<ResponseModel<int>> UpdateBaiduTranslationAPIConfiguration(BaiduTranslationAPIVo data)
    {
        var updateCacheDataCommand = new UpdateBaiduTranslationAPIConfigurationCommand(data);
        await PublishAsync(updateCacheDataCommand);
        return updateCacheDataCommand.Result.CreateResult();
    }

    #region 排序

    [Tags("翻译排序")]
    [EndpointSummary("获取配置排序")]
    [EndpointDescription("获取配置排序")]
    [Authorize]
    public async Task<ResponseModel<List<BasicsSelectDto<int>>?>> GetConfigOrdering()
    {
        var translateOptions = new GetConfigorderingQuery();
        await PublishAsync(translateOptions);
        return translateOptions.Result.CreateResult();
    }

    [Tags("翻译排序")]
    [EndpointSummary("修改配置排序")]
    [EndpointDescription("修改配置排序")]
    [Authorize]
    public async Task<ResponseModel<int>> UpdateConfigOrdering(string id, int sort)
    {
        var translateOptions = new UpdateConfigOrderingCommand(id, sort);
        await PublishAsync(translateOptions);
        return translateOptions.Result.CreateResultUpdate();
    }

    #endregion
}